package mobilesafe.guoshisp.com.xdtong.util;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.InputStream;

import mobilesafe.guoshisp.com.xdtong.entity.StudentInfo;

/**
 * Description:解析Html数据，
 * User: Dream_Coder(chenchen_839@126.com)
 * Date: 2015-07-18
 * Time: 23:40
 */
public class ParseHtml {
    /**
     * 从获取到的html页面中解析属于学生信息的数据
     * @param content
     * @return StudentInfo 学生信息类
     */
    public static StudentInfo parseStudentInfo(StringBuffer content) {
        int  i = 1;
        String temp;
        Document document = Jsoup.parse(content.toString());
        Elements elements = document.getElementsByClass("thumbnail");
        StudentInfo studentInfo = new StudentInfo();
        for (Element element : elements) {
           switch (i) { /*通过i来判断所选分组，便于将需要的数据选出来*/
               case 1:
                   Elements personalInfo = element.select("label");
                   studentInfo.setName(personalInfo.get(1).text());
                   studentInfo.setStudentNum(personalInfo.get(2).text());
                   studentInfo.setSex(personalInfo.get(3).text());
                   i++;
                   break;
               case 2:
                   Elements grade = element.select("td");
                   studentInfo.setSumLength(grade.get(1).text());
                   studentInfo.setAverageVelocity(grade.get(3).text());
                   studentInfo.setValidTimes(grade.get(5).text());
                   i++;
                   break;
               case 3:
                   Elements standard = element.select("td");
                   studentInfo.setLowVelocity(standard.get(3).text());
                   studentInfo.setGroup(standard.get(1).text());
                   studentInfo.setLowLength(standard.get(5).text());
                   studentInfo.setLowTimes(standard.get(7).text());
                   break;
           }
        }
        return studentInfo;
    }
}
